package com.asion.qkan.adapters;

import android.annotation.TargetApi;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.asion.qkan.R;
import com.asion.qkan.entry.NewBean;
import com.asion.qkan.ui.activity.SheQuActivity;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class NewAdapter extends RecyclerView.Adapter<NewAdapter.MyHolder> {
    private Context context;
    private List<NewBean.DataBean.FeedsBean> listbean;

    public NewAdapter(Context context) {
        this.context = context;
        listbean = new ArrayList<>();
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.new_item, parent, false);
        MyHolder myHolder = new MyHolder(view);
        view.setOnClickListener(myHolder);
        return myHolder;
    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
        holder.feedsBean = listbean.get(position);
        NewBean.DataBean.FeedsBean bean = holder.feedsBean;
        holder.newName.setText(bean.getUser().getNickname());
        holder.newText.setText(bean.getContent().getText());
        holder.newTime.setText(bean.getCreated_at() + "");
        holder.newComments.setText(bean.getComments_count() + "");
        holder.newLike.setText(bean.getLikes_count() + "");
        holder.newTouxiang.setImageURI(bean.getUser().getAvatar_url());
        LayoutInflater inflater = LayoutInflater.from(context);
        if (bean.getContent().getImages() != null){
            if (bean.getContent().getImages().size() != 0) {
                View view = inflater.inflate(R.layout.remen_item_image, holder.newLinear, false);
                holder.image = (SimpleDraweeView) view.findViewById(R.id.remen_item_image);
                holder.image.setImageURI(bean.getContent().getImage_base()+bean.getContent().getImages().get(0));
                holder.newLinear.addView(view);
            }
        }
    }

    @Override
    public int getItemCount() {
        return listbean.size();
    }

    public void addAll(Collection collection) {
        listbean.addAll(collection);
        notifyDataSetChanged();
    }

    public static class MyHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private NewBean.DataBean.FeedsBean feedsBean;
        @BindView(R.id.new_touxiang)
        SimpleDraweeView newTouxiang;
        @BindView(R.id.new_name)
        TextView newName;
        @BindView(R.id.new_watch)
        ImageView newWatch;
        @BindView(R.id.new_text)
        TextView newText;
        @BindView(R.id.new_linear)
        LinearLayout newLinear;
        @BindView(R.id.new_time)
        TextView newTime;
        @BindView(R.id.new_comments)
        TextView newComments;
        @BindView(R.id.new_like)
        TextView newLike;
        SimpleDraweeView image;
        private Dialog dialog;

        public MyHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
            newLinear.setOnClickListener(this);
            newWatch.setOnClickListener(this);
            dialog = new Dialog(itemView.getContext(),R.style.Dialog);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.new_linear:
                    show();
                    break;
                case R.id.new_watch:
                    view.setVisibility(View.GONE);
                    break;
                default:
                    Bundle bundle = new Bundle();
                    shuju(bundle);
                    Intent intent = new Intent();
                    intent.setClass(view.getContext(), SheQuActivity.class);
                    intent.putExtras(bundle);
                    view.getContext().startActivity(intent);
                    break;
            }
        }

        private void shuju(Bundle bundle) {
            bundle.putString("name",feedsBean.getUser().getNickname());
            bundle.putString("touxiang",feedsBean.getUser().getAvatar_url());
            bundle.putString("text",feedsBean.getContent().getText());
            bundle.putString("time",feedsBean.getCreated_at()+"");
            bundle.putString("comments",feedsBean.getComments_count()+"");
            bundle.putString("like",feedsBean.getLikes_count()+"");
            if (image != null ){
                bundle.putInt("n",1);
                bundle.putString("image",feedsBean.getContent().getImage_base()+feedsBean.getContent().getImages().get(0));
            }else{
                bundle.putInt("n",-1);
            }
        }

        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        private void show() {
            dialog.setContentView(R.layout.dialog_item);
            SimpleDraweeView dia_image = (SimpleDraweeView) dialog.findViewById(R.id.dig_image);
            Drawable drawable = this.image.getDrawable();
            dia_image.setBackground(drawable);
            dialog.show();
        }
    }
}
